﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.DirectoryServices;
using System.Collections;
using System.Web.Caching;


public class ADHelper
{
    public static String GetFullName(String LoginDomain,String username)
    {
        String fullName, firstName, lastName;

        DirectoryEntry searchRoot = new DirectoryEntry("LDAP://RootDSE");
        
        //string domain = searchRoot.Properties["defaultNamingContext"][0].ToString();
        string domain = LoginDomain.ToString();
        searchRoot = new DirectoryEntry("LDAP://" + domain);
        DirectorySearcher search = new DirectorySearcher(searchRoot);
        search.ServerTimeLimit = new TimeSpan(0, 1, 0, 0, 0);
        search.Filter = "(&(objectClass=person) (samaccountname=" + username + "))";
        SearchResult result = search.FindOne();
        DirectoryEntry resultEntry = new DirectoryEntry();
        resultEntry = result.GetDirectoryEntry();

        if (resultEntry.Properties["givenname"].Value == null)
        {
            firstName = "";
        }
        else
        {
            firstName = resultEntry.Properties["givenname"].Value.ToString();
        }

        if (resultEntry.Properties["sn"].Value == null)
        {
            lastName = "";
        }
        else
        {
            lastName = resultEntry.Properties["sn"].Value.ToString();
        }
        
        fullName = firstName + " " + lastName;
        return fullName;
    }
    public static String GetEMailAddress(String LoginDomain, String username)
    {
        String EMailAddress;

        DirectoryEntry searchRoot = new DirectoryEntry("LDAP://RootDSE");

        //string domain = searchRoot.Properties["defaultNamingContext"][0].ToString();
        string domain = LoginDomain.ToString();
        searchRoot = new DirectoryEntry("LDAP://" + domain);
        DirectorySearcher search = new DirectorySearcher(searchRoot);
        search.ServerTimeLimit = new TimeSpan(0, 1, 0, 0, 0);
        search.Filter = "(&(objectClass=person) (samaccountname=" + username + "))";
        SearchResult result = search.FindOne();
        DirectoryEntry resultEntry = new DirectoryEntry();
        resultEntry = result.GetDirectoryEntry();

        if (resultEntry.Properties["mail"].Value == null)
        {
            EMailAddress = "";
        }
        else
        {
            EMailAddress = resultEntry.Properties["mail"].Value.ToString();
        }
        return EMailAddress;
    } 
    public static bool UserExists(string UserName)
    {
        //create an instance of the DirectoryEntry 
        DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE");
        //create instance fo the direcory searcher 

        DirectorySearcher deSearch = new DirectorySearcher();
        //set the search filter 

        deSearch.SearchRoot = de;
        deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";

        //find the first instance 
        SearchResultCollection results = deSearch.FindAll();
        //if the username and password do match, then this implies a valid login 
        //if so then return the DirectoryEntry object 
        if (results.Count == 0)
        {
            return false;
        }

        else
        {
            return true;
        }
    }
    public static bool UserIsMemberOfGroup(string GroupName, string UserName)
    {
        bool IsMemberOf = false;           

        DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE");
        string domain = de.Properties["defaultNamingContext"][0].ToString();
        de = new DirectoryEntry("LDAP://" + domain);
        DirectorySearcher deSearch = new DirectorySearcher(de);
        deSearch.ServerTimeLimit = new TimeSpan(0, 1, 0, 0, 0);

        //set the search filter 
        deSearch.SearchRoot = de;
        deSearch.PropertiesToLoad.Add("cn"); 
        deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName + "))";
  
        SearchResult results = deSearch.FindOne();        

        
        if (results != null)
        { 
            DirectoryEntry deGroup = new DirectoryEntry("LDAP://RootDSE");
            System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
            int n = pcoll["member"].Count;

            //if there are members fo the group, then get the details and assign to the table 

            for (int l = 0; l < n; l++)
            { 
                DirectoryEntry deUser = new DirectoryEntry("LDAP://RootDSE");
                DirectoryEntry deGroupUsers = new DirectoryEntry("LDAP://RootDSE");
                DirectorySearcher deUserSearch = new DirectorySearcher();
                deUserSearch.SearchRoot = deUser;
                deUserSearch.Filter = "(&(objectClass=user)(cn=" + "John Rieder" + "))";
                //SearchResultCollection userResults = deUserSearch.FindAll(); 

                SearchResult userResult = deUserSearch.FindOne();
                DirectoryEntry resultEntry = new DirectoryEntry();
                resultEntry = userResult.GetDirectoryEntry();

                string UserDisplayName = resultEntry.Properties["cn"].Value.ToString();

                if (UserDisplayName == UserName)
                {
                    IsMemberOf = true; 
                    break;
                }
                else
                {
                    IsMemberOf = false;
                }
            }
        }
        return IsMemberOf;
    }
}  
